﻿using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using KlavoNotifications.Fetch;
using KlavoNotifications.Utils;
using KlavoNotifications.ViewModels.Notification;
using KlavoNotifications.ViewModels.Tray;
using KlavoNotifications.Views.Notification;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;

namespace KlavoNotifications.ViewModels
{
    public abstract class GamesListViewModel : NotificationObject
    {
        protected readonly ObservableCollection<TrayItemViewModel> _items;
        private readonly DelegateCommand<TrayItemViewModel> _navigateCommand;
        protected readonly DispatcherTimer _timer;
        private readonly DelegateCommand _notificationCommand;

        protected GamesListViewModel()
        {
            _items = new ObservableCollection<TrayItemViewModel>();
            _navigateCommand = new DelegateCommand<TrayItemViewModel>(NavigateCommandExecuted);
            _notificationCommand = new DelegateCommand(NotificationCommandExecuted);

            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Tick += Timer_Tick;
        }

        public ObservableCollection<TrayItemViewModel> Items
        {
            get { return _items; }
        }

        public DelegateCommand<TrayItemViewModel> NavigateCommand
        {
            get { return _navigateCommand; }
        }


        public DelegateCommand NotificationCommand
        {
            get { return _notificationCommand; }
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            foreach (var xItem in _items.Where(r => r.IsStarted).ToList())
            {
                if (xItem.Seconds >= 1)
                    xItem.Seconds -= 1;
//                else
//                    _items.Remove(xItem);
            }
        }

        protected TrayItemViewModel ItemFromData(Game game, int timestamp)
        {
            TrayItemViewModel result = new TrayItemViewModel();

            if (game.Voc != null)
                result.Voc = game.Voc.Name;
            else
                result.Voc = ConvertUtils.ConvertGameTypeToString(game.GameType);

            result.PlayersCount = game.Players.Count;

            foreach (var xPlayer in game.Players)
            {
                result.Players.Add(new TrayPlayerViewModel(xPlayer));
            }

            result.Url = String.Format("http://klavogonki.ru/play/?gmid={0}", game.Id);

            if (game.BeginTime == 0)
            {
                if (String.IsNullOrEmpty(game.Timeout))
                    return null;

                result.Seconds = Int32.Parse(game.Timeout);
                result.IsStarted = false;
            }
            else
            {
                result.Seconds = game.BeginTime - timestamp;
                result.IsStarted = true;
            }

            result.IsCompetition = !String.IsNullOrEmpty(game.Competition) && game.Competition != "0";
            result.GameType = game.GameType;

            return result;
        }

        protected virtual void NavigateCommandExecuted(TrayItemViewModel item)
        {
            if (item != null && !String.IsNullOrEmpty(item.Url))
                Process.Start(new ProcessStartInfo(item.Url));
        }

        private void NotificationCommandExecuted()
        {
            NotificationViewModel viewModel = new NotificationViewModel();

            NotificationView view = new NotificationView(viewModel);

            Window window = new Window();
            window.Width = 400;
            window.Height = 425;
            window.ResizeMode = ResizeMode.NoResize;
            window.ShowInTaskbar = true;
            window.Topmost = false;
            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            window.Title = "Настройка уведомлений";
            Uri iconUri = new Uri("pack://application:,,,/Icons/klav.ico", UriKind.RelativeOrAbsolute);
            window.Icon = BitmapFrame.Create(iconUri);

            window.Content = view;

            window.ShowDialog();
        }
    }
}